﻿'Copyright (C) 2010  Thomas Huebner
'
'This program is free software
'
'you can redistribute it and/or modify it under the terms 
'of the GNU General Public License as published by the 
'Free Software Foundation either version 2 of the License, 
'or (at your option) any later version.
'
'This program is distributed in the hope that it will be 
'useful, but WITHOUT ANY WARRANTY without even the implied 
'warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
'See the GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License 
'along with this program; if not, write to the 
'Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
'Boston, MA 02111-1307, USA. 


Imports System.Net
Imports System.Text

Public Class LF_Connect

    ''' <summary>
    ''' BaseURL for Liquid Feedback
    ''' </summary>
    ''' <remarks></remarks>
    Private _BaseURL As String

    ''' <summary>
    ''' Personal Token for Liquid Feedback
    ''' </summary>
    ''' <remarks></remarks>
    Private _Token As String


    Private _Areas As LF_AreaList

    ''' <summary>
    ''' Holds the Area list
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Areas() As LF_AreaList
        Get
            Return _Areas
        End Get
    End Property

    Private _Initiatives As LF_InitiativeList

    ''' <summary>
    ''' Holds the Initiatives list
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Initiatives() As LF_InitiativeList
        Get
            Return _Initiatives
        End Get
    End Property

    Private _Suggestions As LF_SuggestionList

    ''' <summary>
    ''' Holds the Initiatives list
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Suggestions() As LF_SuggestionList
        Get
            Return _Suggestions
        End Get
    End Property


    ''' <summary>
    ''' Konstructor for Liquid Feedback Api Connection
    ''' </summary>
    ''' <param name="BaseURL">BaseURL for specific Liquid Feedback server as string</param>
    ''' <param name="Token">Personal Token as string</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal BaseURL As String, ByVal Token As String)
        Me._BaseURL = BaseURL
        Me._Token = Token
        Me._Areas = New LF_AreaList
        Me._Initiatives = New LF_InitiativeList
        Me._Suggestions = New LF_SuggestionList
    End Sub


#Region "Area specific functions"

    ''' <summary>
    ''' Get Area list without parameters
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks></remarks>
    Public Function GetAllAreas() As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Area, Me._BaseURL, Me._Token)
        Return GetAreas(param)

    End Function


    ''' <summary>
    ''' Get a single Area by given ID
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Areas property</remarks>
    Public Function GetAreaByID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Area, Me._BaseURL, Me._Token)
        param.AddParameter("id", ID.ToString)
        Return GetAreas(param)

    End Function


    ''' <summary>
    ''' Get Area list from given ID onwards
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Areas property</remarks>
    Public Function GetAreaFromID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Area, Me._BaseURL, Me._Token)
        param.AddParameter("min_id", ID.ToString)
        Return GetAreas(param)

    End Function


    ''' <summary>
    ''' Get Area list from given ID downwards
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Areas property</remarks>
    Public Function GetAreaToID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Area, Me._BaseURL, Me._Token)
        param.AddParameter("max_id", ID.ToString)
        Return GetAreas(param)

    End Function


    ''' <summary>
    ''' Get Area list with given parameters in parameterobject
    ''' </summary>
    ''' <param name="Parameters"></param>
    ''' <returns>True if no error</returns>
    ''' <remarks></remarks>
    Public Function GetAreas(ByRef Parameters As LF_Parameters) As Boolean

        Try

            Using wc As New WebClient

                'Liquid Feedback is UTF-8 Encoded
                wc.Encoding = Encoding.UTF8

                'Get XML String
                Dim strContent As String = wc.DownloadString(Parameters.GetURL)

                'Deserialize the XML
                Me._Areas = LFNETApi.LF_AreaListConverter.ReadAreaList(strContent)

            End Using

            Return True

        Catch ex As Exception

            Return False

        End Try

    End Function

#End Region

#Region "Initiative specific functions"

    ''' <summary>
    ''' Get All Initiatives 
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Initiatives property</remarks>
    Public Function GetAllInitiatives() As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Initiative, Me._BaseURL, Me._Token)
        Return GetInitiatives(param)

    End Function


    ''' <summary>
    ''' Get single Initiative by given ID
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Initiatives property</remarks>
    Public Function GetInitiativeByID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Initiative, Me._BaseURL, Me._Token)
        param.AddParameter("id", ID.ToString)
        Return GetInitiatives(param)

    End Function


    ''' <summary>
    ''' Get Initiatives by given Issue ID
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Initiatives property</remarks>
    Public Function GetInitiativeByIssueID(ByVal IssueID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Initiative, Me._BaseURL, Me._Token)
        param.AddParameter("issue_id", IssueID.ToString)
        Return GetInitiatives(param)

    End Function


    ''' <summary>
    ''' Get a list of initiatives from given ID onwards
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Initiatives property</remarks>
    Public Function GetInitiativeFromID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Initiative, Me._BaseURL, Me._Token)
        param.AddParameter("min_id", ID.ToString)
        Return GetInitiatives(param)

    End Function


    ''' <summary>
    ''' Get a list of initiatives from given ID downwards
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Initiatives property</remarks>
    Public Function GetInitiativeToID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Initiative, Me._BaseURL, Me._Token)
        param.AddParameter("max_id", ID.ToString)
        Return GetInitiatives(param)

    End Function


    ''' <summary>
    ''' Get Initiatives list with given parameters in parameterobject
    ''' </summary>
    ''' <param name="Parameters"></param>
    ''' <returns>True if no error</returns>
    ''' <remarks></remarks>
    Public Function GetInitiatives(ByRef Parameters As LF_Parameters) As Boolean

        Try

            Using wc As New WebClient

                'Liquid Feedback is UTF-8 Encoded
                wc.Encoding = Encoding.UTF8

                'Get XML String
                Dim strContent As String = wc.DownloadString(Parameters.GetURL)

                'Deserialize the XML
                Me._Initiatives = LFNETApi.LF_InitiativeListConverter.ReadInitiativeList(strContent)

            End Using

            Return True

        Catch ex As Exception

            Return False

        End Try

    End Function

#End Region

#Region "Suggestion specific functions"

    ''' <summary>
    ''' Get all Suggestions
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Suggestions property</remarks>
    Public Function GetAllSuggestions() As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Suggestion, Me._BaseURL, Me._Token)
        Return GetSuggestions(param)

    End Function


    ''' <summary>
    ''' Get a Suggestion by given ID
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Suggestions property</remarks>
    Public Function GetSuggestionByID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Suggestion, Me._BaseURL, Me._Token)
        param.AddParameter("id", ID.ToString)
        Return GetSuggestions(param)

    End Function


    ''' <summary>
    ''' Get Suggestions by given Initiative ID
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Suggestions property</remarks>
    Public Function GetSuggestionByInitiativeID(ByVal InitiativeID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Suggestion, Me._BaseURL, Me._Token)
        param.AddParameter("initiative_id", InitiativeID.ToString)
        Return GetSuggestions(param)

    End Function


    ''' <summary>
    ''' Get a list of Suggestions from given ID onwards
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Suggestions property</remarks>
    Public Function GetSuggestionFromID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Suggestion, Me._BaseURL, Me._Token)
        param.AddParameter("min_id", ID.ToString)
        Return GetSuggestions(param)

    End Function


    ''' <summary>
    ''' Get a list of Suggestions from given ID downwards
    ''' </summary>
    ''' <returns>True if no error</returns>
    ''' <remarks>After calling this function the result is in Suggestions property</remarks>
    Public Function GetSuggestionToID(ByVal ID As Integer) As Boolean

        Dim param As New LF_Parameters(LF_Parameters.RequestType.Suggestion, Me._BaseURL, Me._Token)
        param.AddParameter("max_id", ID.ToString)
        Return GetSuggestions(param)

    End Function


    ''' <summary>
    ''' Get Suggestions list with given parameters in parameterobject
    ''' </summary>
    ''' <param name="Parameters"></param>
    ''' <returns>True if no error</returns>
    ''' <remarks></remarks>
    Public Function GetSuggestions(ByRef Parameters As LF_Parameters) As Boolean

        Try

            Using wc As New WebClient

                'Liquid Feedback is UTF-8 Encoded
                wc.Encoding = Encoding.UTF8

                'Get XML String
                Dim strContent As String = wc.DownloadString(Parameters.GetURL)

                'Deserialize the XML
                Me._Suggestions = LFNETApi.LF_SuggestionListConverter.ReadSuggestionList(strContent)

            End Using

            Return True

        Catch ex As Exception

            Return False

        End Try

    End Function

#End Region


End Class

